﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SafeScript.MetaModel.Statements
{
    public class NewOperator : Operator
    {
        public ITypeDef Type;
        public Operator Initial;
        public Constructor Constructor;

        public override int GetPrecedence()
        {
            return 20;
        }

        public override ITypeDef ComputeType(ITypeDef goal, bool needsRef, ErrorReport report)
        {
            // TODO: Type check the initial ...

            if (!(this.Type is Class))
            {
                report.Add(new ErrorEntry(this.Node) { Type = ErrorType.NotAClass });
                return null;
            }

            if ((this.Type as Class).IsAbstract)
            {
                report.Add(new ErrorEntry(this.Node) { Info = (this.Type as Class).Name, Type = ErrorType.InstantiationOfAbstractClass });
                return null;
            }

            return new InstanceType() { Type = this.Type };
        }
    }
}
